import Location from '@react-native-community/geolocation';

const _config = {
    timeout: 2000
}

let _latitude = 0;
let _longitude = 0;
let isRequest:boolean = false;

interface Res {
    latitude: number,
    longitude: number
}

//监听位置
export default (): Promise<Res> => (
    new Promise((resolve) => {
        const _resolve = () => {
            isRequest = true;
            resolve({
                latitude: _latitude,
                longitude: _longitude
            })
        }

        if (isRequest || !!_latitude || !!_longitude) {
            return _resolve();
        }

        const _success = ({coords}:any) => {
            const {
                latitude = 0,
                longitude = 0
            } = coords;
            _latitude = latitude;
            _longitude = longitude;
            _resolve();
        }
        Location.getCurrentPosition(
            _success, _resolve, _config
        )
    })
)
